package com.ruoyi.infoIntroduce.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.infoSiteService.DTO.IntroduceDTO;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.infoIntroduce.domain.InfoIntroduce;
import com.ruoyi.infoIntroduce.service.IInfoIntroduceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 导师Controller
 * 
 * @author ruoyi
 * @date 2024-04-12
 */

@RestController
@RequestMapping("/InfoIntroduce/introduce")
public class InfoIntroduceController extends BaseController
{
    @Autowired
    private IInfoIntroduceService infoIntroduceService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询导师列表
     */
    @PreAuthorize("@ss.hasPermi('InfoIntroduce:introduce:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoIntroduce infoIntroduce)
    {
        startPage();
        List<InfoIntroduce> list = infoIntroduceService.selectInfoIntroduceList(infoIntroduce);
        return getDataTable(list);
    }

    /**
     * 导出导师列表
     */
    @PreAuthorize("@ss.hasPermi('InfoIntroduce:introduce:export')")
    @Log(title = "导师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoIntroduce infoIntroduce)
    {
        List<InfoIntroduce> list = infoIntroduceService.selectInfoIntroduceList(infoIntroduce);
        ExcelUtil<InfoIntroduce> util = new ExcelUtil<InfoIntroduce>(InfoIntroduce.class);
        util.exportExcel(response, list, "导师数据");
    }

    /**
     * 获取导师详细信息
     */
    @PreAuthorize("@ss.hasPermi('InfoIntroduce:introduce:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(infoIntroduceService.selectInfoIntroduceById(id));
    }

    /**
     * 新增导师
     */
    @Log(title = "导师", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntroduceDTO introduceDTO)
    {
        InfoIntroduce infoIntroduce = introduceDTO.getInfoIntroduce();
        //取出user设置账号密码新建用户
        SysUser user = introduceDTO.getUser();
        if (StringUtils.isNotNull(user)){
            if (!userService.checkUserNameUnique(user)) {
                return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
            }
            int i = userService.insertUser(user);
        }
        SysUser user1 = userService.selectUserByUserName(user.getUserName());
        //获取userId
        infoIntroduce.setUserId(user1.getUserId());
        infoIntroduceService.insertInfoIntroduce(infoIntroduce);
        return AjaxResult.success("导师新增成功");
    }

    /**
     * 修改导师
     */
    @PreAuthorize("@ss.hasPermi('InfoIntroduce:introduce:edit')")
    @Log(title = "导师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoIntroduce infoIntroduce)
    {
        return toAjax(infoIntroduceService.updateInfoIntroduce(infoIntroduce));
    }

    /**
     * 删除导师
     */
    @PreAuthorize("@ss.hasPermi('InfoIntroduce:introduce:remove')")
    @Log(title = "导师", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(infoIntroduceService.deleteInfoIntroduceByIds(ids));
    }


}
